package com.yuwei.live.view

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.CountDownTimer
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.WindowManager
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.PopupWindow
import android.widget.TextView
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.yuwei.common.mvi.BaseMviFragment
import com.yuwei.common.mvi.BaseViewFragment
import com.yuwei.common.mvi.UiState
import com.yuwei.live.LiveIntent
import com.yuwei.live.LiveViewModel
import com.yuwei.live.R
import com.yuwei.live.adapter.BottomHotAdapter
import com.yuwei.live.adapter.TopHotAdapter
import com.yuwei.live.databinding.FragmentAllBinding
import com.yuwei.live.entity.TopHotEntity
import java.text.SimpleDateFormat
import java.util.*


class AllFragment : BaseMviFragment<FragmentAllBinding,LiveViewModel>() {
    lateinit var countDownTimer : CountDownTimer
    private val topAdapter = TopHotAdapter()
    private val bottomAdapter = BottomHotAdapter()
    private val topHotList = listOf(
        TopHotEntity("熊猫哥", R.drawable.two, true),
        TopHotEntity("产品研究", R.drawable.one, false),
        TopHotEntity("狮子王", R.drawable.three, true),
    )

    override fun successData(it: UiState.onSuccess<*>) {
        when(it.type){
            "getRankingList" -> {
                val data = it.data as com.yuwei.live.entity.RankingEntity
                bottomAdapter.data.addAll(data)
                bottomAdapter.data.removeAt(0)
                bottomAdapter.data.removeAt(1)
                bottomAdapter.data.removeAt(2)
                bottomAdapter.data.removeAt(3)
                bottomAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun failureData(error: String) {

    }

    override fun initView() {
        vm.sendIntent(LiveIntent.getRankingList)
        topAdapter.data.addAll(topHotList)
        binding.topRv.adapter = topAdapter
        binding.topRv.layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.HORIZONTAL,false)
        binding.bottomRv.adapter = bottomAdapter
        binding.bottomRv.layoutManager = LinearLayoutManager(requireContext())
        val inflate = LayoutInflater.from(requireContext()).inflate(R.layout.rule_layout, null)
        val popupWindow = PopupWindow(
            inflate,
            ViewGroup.LayoutParams.MATCH_PARENT,
            900
        )
        popupWindow.isFocusable = true
        binding.rule.setOnClickListener {
            popupWindow.contentView.translationX = 900f // 下滑距离（根据需求调整
            popupWindow.contentView.animate()
                .translationX(0f)
                .setInterpolator(LinearInterpolator())
                .setDuration(500)
                .start()
            popupWindow.showAsDropDown(
                binding.view,
                Gravity.BOTTOM,
                0,
                0,
            )
        }
        val back = inflate.findViewById<ImageView>(R.id.rule_back)
        back.setOnClickListener {
            popupWindow.dismiss()
        }
    }

    override fun initData() {
        bottomAdapter.setOnItemClickListener { _, _, position ->
            XPopup.Builder(requireContext())
                .autoDismiss(false) // 点击按钮不自动关闭
                .hasShadowBg(true) // 显示半透明背景
                .asConfirm(
                    null,
                    "确定要跳转到 ${bottomAdapter.data[position].room_name} 的直播间么?",
                    "取消", // 确认按钮文本
                    "确定不再提醒",  // 取消按钮文本
                    {
                        // 确认回调
                        ToastUtils.showShort("确定跳转")
                    },
                    {
                        // 取消回调
                        ToastUtils.showShort("取消跳转")
                    },
                    false, // 是否隐藏取消按钮
                )
                .show()
        }
    }

    override fun onResume() {
        super.onResume()
        val calendar = Calendar.getInstance()
        val currentMinute = calendar.get(Calendar.MINUTE)
        val currentSecond = calendar.get(Calendar.SECOND)
        countDownTimer = object : CountDownTimer((3600 - (currentMinute * 60 + currentSecond))* 1000.toLong(), 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val sdf = SimpleDateFormat("mm:ss")
                binding.countdownTime.text = "倒计时 ${sdf.format(millisUntilFinished)}"
            }

            override fun onFinish() {

            }
        }.start()
    }

    override fun onPause() {
        super.onPause()
        countDownTimer.cancel()
    }
}